package org.jeecg.modules.qyxx.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.jeecg.modules.basesource.entity.DjPzjgxx;
import org.jeecg.modules.basesource.service.IDjNsrzyxxService;
import org.jeecg.modules.basesource.service.IDjPzjgxxService;
import org.jeecg.modules.dzswj.vo.response.DzswjBaseResponse;
import org.jeecg.modules.dzswj.vo.response.PzjgxxVo;
import org.jeecg.modules.dzswj.vo.response.nsrxxcx.Kzxx;
import org.jeecg.modules.dzswj.vo.response.nsrxxcx.Nsrjbxx;
import org.jeecg.modules.dzswj.vo.response.nsrxxcx.Nsrkzxx;
import org.jeecg.modules.dzswj.vo.response.nsrxxcx.NsrxxCxResponseVo;
import org.jeecg.modules.qyxx.entity.DjNsrxxPO;
import org.jeecg.modules.qyxx.mapper.DjNsrxxMapper;
import org.jeecg.modules.qyxx.service.IDjNsrxxService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Description: ...
 * @Author: ZJJ
 * @Date:   2021-06-01
 * @Version: V1.0
 */
// @DS("multi-datasource1")申报
@Service
public class DjNsrxxServiceImpl extends ServiceImpl<DjNsrxxMapper, DjNsrxxPO> implements IDjNsrxxService {

    @Autowired
    private IDjNsrzyxxService djNsrzyxxService;

    @Autowired
    private IDjPzjgxxService djPzjgxxService;

    @Override
    public DjNsrxxPO getOne(){
        LambdaQueryWrapper<DjNsrxxPO> wrapper = Wrappers.lambdaQuery(DjNsrxxPO.class);
        wrapper.eq(DjNsrxxPO::getAreaCode,1);
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public DjNsrxxPO getByNsrsbhOrShxydm(String nsrsbhOrShxydm) {
        LambdaQueryWrapper<DjNsrxxPO> wrapper = Wrappers.lambdaQuery(DjNsrxxPO.class);
        wrapper.eq(DjNsrxxPO::getNsrsbh, nsrsbhOrShxydm).or().eq(DjNsrxxPO::getShxydm, nsrsbhOrShxydm);
        return baseMapper.selectOne(wrapper);
    }

    @Transactional
    @Override
    public DjNsrxxPO saveOrUpdateNsrjbxxAndPzjg(NsrxxCxResponseVo responseVo, List<PzjgxxVo> pzjgList) {
        Nsrjbxx nsrjbxx = responseVo.getNsrjbxx();
        Kzxx.Nsrjbxx nsrkzxx = responseVo.getKzxx().getNsrjbxx();
        JSONObject jbxxJson = (JSONObject) JSONObject.toJSON(nsrjbxx);
        JSONObject nsrkzxxJson = (JSONObject) JSONObject.toJSON(nsrkzxx);
        jbxxJson.putAll(nsrkzxxJson);
        DjNsrxxPO djNsrxx = JSONObject.parseObject(JSON.toJSONString(jbxxJson), DjNsrxxPO.class);
        djNsrxx.setTynsrsbh(StringUtils.isNotBlank(djNsrxx.getShxydm()) ? djNsrxx.getShxydm() : djNsrxx.getNsrsbh());
        djNsrxx.setGdslxDm("01");
        djNsrxx.setSjcjSj(new Date());
        this.saveOrUpdate(djNsrxx);

        //保存扩展信息
        djNsrzyxxService.saveOrUpdateFromKzxx(djNsrxx.getTynsrsbh(), nsrkzxx);
        //更新批准机构信息
        if (pzjgList != null && pzjgList.size() != 0) {
            for (PzjgxxVo pzjgxx : pzjgList) {
                DjPzjgxx djPzjgxx = djPzjgxxService.selectByTynsrsbhAndUuid(djNsrxx.getTynsrsbh(), pzjgxx.getPzjguuid());
                if (djPzjgxx == null) {
                    djPzjgxx = new DjPzjgxx();
                    BeanUtils.copyProperties(pzjgxx, djPzjgxx);
                    djPzjgxx.setTynsrsbh(djNsrxx.getTynsrsbh());

                    djPzjgxxService.save(djPzjgxx);
                }
            }
        }
        //保存
        return djNsrxx;
    }
}
